import { GameData } from "./GameData";
import { AnimalDatas, JumpSpace } from "./GameDefine";
import { EventKey } from "../common/Key";

export const GameState = {
    init() {
        this._speedScale = 1;
        this._productionSpeed = 0;
    },

    getCanBuyByCoin(index) {
        // 第一个可以金币购买
        if (index == 0) {
            return true;
        }

        // 如果全部解锁了，则全部都可以用金币购买
        let maxIndex = this.getMaxAnimalIndex();
        let currIndex = GameData.getCurrAnimalIndex();
        if (currIndex >= maxIndex) {
            return true;
        }



        // 倒数1-2不可以金币购买
        if (index <= currIndex - 4) {
            return true;
        }

        // 倒数3-4不可以金币购买
        if (index <= currIndex - 2 && index > currIndex - 4) {
            if (index <= currIndex && this.getAnimalDiamondPrice(index) <= 0) {
                return true;
            }
            return false;
        }

        return false;
    },
    getCanBuyByDiamond(index) {
        if (index == 0) {
            return false;
        }
        // 如果全部解锁了，都不可以用钻石购买
        let maxIndex = this.getMaxAnimalIndex();
        let currIndex = GameData.getCurrAnimalIndex();
        if (currIndex >= maxIndex) {
            return false;
        }

        if (this.getAnimalDiamondPrice(index) <= 0) {
            return false;
        }

        // 倒数2，3可以钻石购买
        if (index <= currIndex - 4) {
            return false;
        }

        if (index >= currIndex - 1) {
            return false;
        }


        return true;
    },
    getMaxAnimalIndex() {
        return AnimalDatas.length - 1;
    },
    getMaxIndexCanBuyByCoin() {
        // 如果全部解锁了，则全部都可以用金币购买
        let maxIndex = this.getMaxAnimalIndex();
        let currIndex = GameData.getCurrAnimalIndex();

        if (currIndex >= maxIndex) {
            return maxIndex;
        }

        // 倒数1-5不可以金币购买
        if (currIndex <= 4) {
            return 0;
        }
        return currIndex - 4;
    },

    getAnimalCoinPrice(index) {
        let data = AnimalDatas[index] || {};
        let price = data.price || 0;
        let count = GameData.getAnimalBuyCount(index);
        price = G.bignumber.mult(price, Math.pow(1.07, count));
        price = price.integerValue();
        return G.bignumber.toBigNumber(price);
    },
    getAnimalDiamondPrice(index) {
        let data = AnimalDatas[index] || {};
        let price = data.dprice || 0;
        return price;
    },

    getProductionSpeed() {
        return this._productionSpeed;
    },

    updateProductionSpeed() {
        let aliveAnimalData = GameData.getAliveAnimalData();
        let sum = 0;
        for (let i = 0; i < aliveAnimalData.length; i++) {
            let index = aliveAnimalData[i].getIndex();
            let production = this.getAnimalProduction(index);
            sum = G.bignumber.plus(sum, production);
        }
        this._productionSpeed = G.bignumber.div(sum, JumpSpace / this.getSpeedScale());
        G.onFire.fire(EventKey.EVENT_UPDATE_PRODUCTION_SPEED);
    },
    // 产出
    getAnimalProduction(index) {
        let data = AnimalDatas[index] || {};
        let production = data.production || 0;
        return G.bignumber.toBigNumber(production);
    },

    // 速度系数
    getSpeedScale() {
        return this._speedScale;
    },

    setSpeedScale(scale) {
        this._speedScale = scale;
    },

    // 获取n秒收益
    getProduction(s) {
        let speed = this.getProductionSpeed();
        let coin = G.bignumber.mult(speed, s);
        return coin;
    },
}
